#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
typedef struct
{
    int size;
    int top;
    int data[100000];
}Stack;

void init(Stack *S,int size)
{
    S->size = size;
    S->top = 0;
}

int isFull(Stack *S)
{
    if(S->top == S->size)
        return 1;
    else
        return 0;
}

int isEmpty(Stack *S)
{
    if(S->top == 0)
        return 1;
    else
        return 0;
}

void push(Stack *S,int e)
{     
    S->data[S->top] = e;  
    S->top++;  
}

int pop(Stack *S)
{
    int e;
    S->top--;  	
    e = S->data[S->top];    
    return e;
}
int main()
{
    int m,n,max,min;
    scanf("%d %d",&m,&n);
    max = m;min = n;
    if(m<n)
    {
        max = n;
        min = m;
    }
    Stack S1;
    Stack S2;
    init(&S1,max);
    init(&S2,min);
    char c;
    int a;
    while(1)
    {
        scanf("%s",&c);
        if(c == 'T'){        
            return 0;
        }       
        if(c == 'A')
        {
            scanf("%d",&a);
            if(isFull(&S2)==1 && isEmpty(&S1)==0)//如果S2满，S1非空
            {
                printf("ERROR:Full\n");
                continue;
            }else{
                if(isEmpty(&S1)==1 && isFull(&S2)==1)//如果S1空,S2满，将S2中的数据放入S1
                {
                    int len = S2.size;
                    int e1;
                    for(int i = 0;i<len;i++)
                    {		
                       e1 = pop(&S2);
                       push(&S1,e1);
                    }
                    push(&S2,a);
                }else{
                    push(&S2,a);
                }                
            }
        }
        if(c == 'D')
        {
            int b;
           if(isEmpty(&S2)==1 && isEmpty(&S1)==1)//如果S1,S2都空
           {
               printf("ERROR:Empty\n");
               continue;
           }
            else{
					int len = S2.top;
                    if(isEmpty(&S1)==1)//如果1空了，将2中数据放入1
                    {
                        int e1;
                        while(len--)
                        {		
                            e1 = pop(&S2);
                            push(&S1,e1);
                         } 
                        b = pop(&S1);
					    printf("%d\n",b);
                    }                    			
				    else{
				        b = pop(&S1) ;
					    printf("%d\n",b); 
                    }                         
            }
        }
    }
    return 0;
}
